/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.yy.liuxue.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.adobe.xmp.impl.Utils;
import com.gwc.yy.liuxue.entity.Remind;
import com.gwc.yy.liuxue.service.RemindService;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 提醒Controller
 * @author 黎俊涛
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/liuxue/remind")
public class RemindController extends BaseController {

	@Autowired
	private RemindService remindService;
	
	@ModelAttribute
	public Remind get(@RequestParam(required=false) String id) {
		Remind entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = remindService.get(id);
		}
		if (entity == null){
			entity = new Remind();
		}
		return entity;
	}
	@RequiresPermissions("liuxue:remind:view")
	@RequestMapping(value = {"calendar"})
	public String calendar(HttpServletRequest request, HttpServletResponse response, Model model) {
		//Page<Remind> page = remindService.findPage(new Page<Remind>(request, response), remind); 
		//model.addAttribute("page", page);
		return "yy/liuxue/calendar";
	}

	@RequiresPermissions("liuxue:remind:view")
	@RequestMapping(value = {"list", ""})
	public String list(Remind remind, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Remind> page = remindService.findPage(new Page<Remind>(request, response), remind); 
		model.addAttribute("page", page);
		return "yy/liuxue/remindList";
	}

	@RequiresPermissions("liuxue:remind:view")
	@RequestMapping(value = "form")
	public String form(Remind remind, Model model) {
	   /* User u = new User();
	    u.setName("李四");
	    u.setId("123");
	    remind.getStudents().add(u);*/
		remind.setUser(UserUtils.getUser());
		List<User> users = new ArrayList<User>();
		if(remind.getUser().getUserType().equals("5")){
			users.add(remind.getUser());
		}else{
			users = remindService.findStudentList(remind.getUser().getId());
		}
		System.out.println(remind.getUser().getId()+"=================================================");
		remind.setStudents(users);
		model.addAttribute("remind", remind);
		return "yy/liuxue/remindForm";
	}

	@RequiresPermissions("liuxue:remind:edit")
	@RequestMapping(value = "save")
	public String save(Remind remind, Model model, RedirectAttributes redirectAttributes) {
		remind.setUser(UserUtils.getUser());
		if (!beanValidator(model, remind)){
			return form(remind, model);
		}
		String [] student_ids = remind.getStudent_id();
		remindService.save(remind,student_ids);
		
		
		addMessage(redirectAttributes, "保存提醒成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/remind/calendar";
	}
	
	@RequiresPermissions("liuxue:remind:edit")
	@RequestMapping(value = "delete")
	public String delete(Remind remind, RedirectAttributes redirectAttributes) {
		remindService.delete(remind);
		addMessage(redirectAttributes, "删除提醒成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/remind/?repage";
	}
	@ResponseBody
	@RequestMapping(value = "myremind")
	public List<Remind> myRemind() {
		
		return remindService.findListByUser(UserUtils.getUser());
	}
}